<?php
namespace app\spadmin\controller\system;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
class User extends Backend{

    private $userLevel;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }
    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['mobile|nickname'] = ['like','%'.$data['keywords'].'%'];
            }           

            $map['status'] = 1;         
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
            $userLevel = model('UserLevel')->column('id,name','id');
            foreach ($list['data'] as $key => &$value) {
                $value['user_level'] = $userLevel[$value['level_id']];
                if($value['pid']>0){
                    $puser = model('User')->where('id',$value['pid'])->find();
                    $value['p_user'] = $puser['nickname'].'|'.$puser['mobile'];
                    $value['p_user_level']=$userLevel[$puser['level_id']];
                }else{
                    $value['p_user'] = '-';
                    $value['p_user_level']='-';
                }
                if($value['tid']>0){
                    $tuser = model('User')->where('id',$value['tid'])->find();
                    $value['t_user'] = $tuser['nickname'].'|'.$tuser['mobile'];
                    $value['t_user_level']=$userLevel[$tuser['level_id']];
                }else{
                    $value['t_user'] = '-';
                    $value['t_user_level']='-';
                }
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function detail($ids=null){
        $row = $this->model->get($ids);
        $num = $this->model->where('pid',$row['id'])->where('level_id',">",1)->count();
        $row->inv_num = $num;
        if($row->is_auth == 1){
                   if($row->level_id == 2){
                        if($num >=50){
                            $row->level_id = 4;
                            Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到50人升到资深合伙人'
                            ],true);
                        }else{
                            if($num >=20){
                            $row->level_id = 3;
                            Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到20人升到高级合伙人'
                            ],true);
                            }
                        } 
                   }
                   if($row->level_id == 3){
                       if($num >=50){
                             $row->level_id = 4;
                             Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到50人升到资深合伙人'
                            ],true);
                        } 
                   }
            }
        $row->save();
        $userLevel = model('UserLevel')->column('id,name','id');
        $row['user_level']=$userLevel[$row['level_id']];
        $row['is_attention_txt'] = $row['is_attention']>0?'已关注':'未关注';
        $row['sign_time_txt'] = $row['sign_time']>0?date('Y-m-d H:i',$row['sign_time']):'';
        $this->assign('info',$row);
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            $user = $this->model->where('id',$data['id'])->find();
            
            $is_update = 0;
            if($user['pid'] != $data['pid']){
                $user['pid'] = $data['pid']?:0;
                if($data['pid']>0){
                    $is_update = 1;
                }
            }
            $user['level_id'] = $data['level_id'];
            Db::startTrans();
            try {
                if($is_update == 1){
                    $puser = $this->model->where('id',$data['pid'])->find();
                    $user['tid']=$puser['pid']?:0;
                    $user['is_old']=0;
                    $user->save();
                    $puser['inv_num'] = $this->model->where('pid',$puser['id'])->where('level_id',">",1)->count();
                    $puser->save();
                }else{
                    $user->save();
                }
                Db::commit();
                $this->success('操作成功');
            } catch (\think\exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            
            $num = $this->model->where('pid',$row['id'])->where('level_id',">",1)->count();
            $row->inv_num = $num;
            if($row->is_auth == 1){
                   if($row->level_id == 2){
                        if($num >=50){
                            $row->level_id = 4;
                            Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到50人升到资深合伙人'
                            ],true);
                        }else{
                            if($num >=20){
                            $row->level_id = 3;
                            Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到20人升到高级合伙人'
                            ],true);
                            }
                        } 
                   }
                   if($row->level_id == 3){
                       if($num >=50){
                             $row->level_id = 4;
                             Note::create([
                                'user_id'=>$id,
                                'note'=>'下级合伙人达到50人升到资深合伙人'
                            ],true);
                        } 
                   }
            }
            $row->save();
            $userLevel = model('UserLevel')->column('id,name','id');
            $user = $this->model->where('status',1)->select();
            $this->assign('user',$user);
            $this->assign('level',$userLevel);
            $this->assign('info',$row);
            return $this->view->fetch();
        }
}

    public function tree(){
        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['mobile|nickname'] = ['like','%'.$data['keywords'].'%'];
            }           

            $map['status'] = 1;         
            $list =   $this->model->where($map)->order('id asc')->paginate(999999)->toArray();
            $userLevel = model('UserLevel')->column('id,name','id');
            foreach ($list['data'] as $key => &$value) {
                $value['user_level'] = $userLevel[$value['level_id']];
                if($value['pid']>0){
                    $puser = model('User')->where('id',$value['pid'])->find();
                    $value['p_user'] = $puser['nickname'].'|'.$puser['mobile'];
                    $value['p_user_level']=$userLevel[$puser['level_id']];
                }else{
                    $value['p_user'] = '-';
                    $value['p_user_level']='-';
                }
                if($value['tid']>0){
                    $tuser = model('User')->where('id',$value['tid'])->find();
                    $value['t_user'] = $tuser['nickname'].'|'.$tuser['mobile'];
                    $value['t_user_level']=$userLevel[$tuser['level_id']];
                }else{
                    $value['t_user'] = '-';
                    $value['t_user_level']='-';
                }
            }
            return $this->result($list);
        }
         return $this->view->fetch();
    }
}